<?php
namespace User\Controller;

class ArticleController extends \Common\Controller\UserController {

	public function index($cid = '' , $model_id = null){
		$document = D('Document');
		$builder = new \OT\Builder();

		if ($model_id == null) {
			// 获取基础模型信息
			$model = M('Model')->getByName('document');
			$model_id   =   null;
			$cate_id    =   0;
			$this->assign('model', null);
		}else{
			//获取模型信息
			$model = M('Model')->getById($model_id);
			if (empty($model['list_grid'])) {
				$model['list_grid'] = M('Model')->getFieldByName('document','list_grid');
			}
			$this->assign('model', explode(',', $models));
		}

		//解析列表规则
		$fields =	array();
		$grids  =	preg_split('/[;\r\n]+/s', trim($model['list_grid']));
		foreach ($grids as &$value) {
			// 字段:标题:链接
			$val      = explode(':', $value);
			// 支持多个字段显示
			$field   = explode(',', $val[0]);
			$value    = array('field' => $field, 'title' => $val[1]);
			if(isset($val[2])){
			    // 链接信息
			    $value['href']  =   $val[2];
			    // 搜索链接信息中的字段信息
			    preg_replace_callback('/\[([a-z_]+)\]/', function($match) use(&$fields){$fields[]=$match[1];}, $value['href']);
			}
			if(strpos($val[1],'|')){
			    // 显示格式定义
			    list($value['title'],$value['format'])    =   explode('|',$val[1]);
			}
			foreach($field as $val){
			    $array  =   explode('|',$val);
			    $fields[] = $array[0];
			}
		}

		//$list = $document->field($fields)->select();

		$model_list = get_document_model();
		foreach ($model_list as $key => $value) {
			if ($value['extend'] == 1) {
				$builder->buttonNew(U('User/Article/add',array('model_id'=>$value['id'])),$value['title'].'新增');
			};
		}
		$builder
			->title('文档管理')
			->display();
	}

	public function add(){
		$builder = new \OT\Builder('config');

		$builder
			->title('文档添加')
			->display();
	}
}